package com.bishe.service;

import cn.hutool.core.util.StrUtil;
import com.api.entitis.JobsInfo;
import com.api.entitis.query.JobsInfoQuery;
import com.api.service.IJobsInfoService;
import com.api.util.JobUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bishe.mapper.JobsInfoMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * 招聘信息接口
 */
@Service
public class IJobsInfoServiceImpl extends ServiceImpl<JobsInfoMapper, JobsInfo> implements IJobsInfoService {

    @Override
    public void addOrUpdate(JobsInfo jobsInfo) {

        if (StrUtil.isEmpty(jobsInfo.getId()) || getById(jobsInfo.getId()) == null){
            // 添加
                jobsInfo.setId(UUID.randomUUID().toString());
                jobsInfo.setCreateBy(JobUtils.getLoginId());
                jobsInfo.setCreateTime(new Date());
                jobsInfo.setUpdateTime(new Date());
                save(jobsInfo);
                return;
        }

        // 修改
        jobsInfo.setUpdateTime(new Date());
        updateById(jobsInfo);
    }

    @Override
    public void delete(String id) {
        JobsInfo byId = getById(id);
        if (byId != null){
            this.removeById(id);
        }
    }




    @Override
    public IPage<JobsInfo> toPage(JobsInfoQuery query) {

        LambdaQueryWrapper<JobsInfo> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(query.getPost())){
            wrapper.like(JobsInfo::getPost,query.getPost());
        }

        wrapper.orderByDesc(JobsInfo::getUpdateTime);

        //根据修改时间倒叙
        IPage<JobsInfo> resultPage = this.page(query.toPage(),wrapper);

        return resultPage;
    }
}
